package com.mi.proxy4;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class Test2 {

	public static void main(String[] args) throws Exception {
		
		Class<?>[] interfaces = new Class<?>[]{Singer.class,Person.class};
		Object obj = Proxy.newProxyInstance(Test2.class.getClassLoader(), interfaces, new MyInvocationHandler());
		System.err.println(obj.getClass());
		System.out.println(obj instanceof Singer);
		System.out.println(obj instanceof Person);
		
		Singer singer = (Singer)obj;
		singer.sing("小苹果");
		singer.sign();
		singer.work();
		
		Person person = (Person)obj;
		int index = person.lives();
		System.out.println(index);
	}

}


class MyInvocationHandler implements InvocationHandler{

	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		String methodName = method.getName();
		switch (methodName) {
		case "sing":
			System.out.println(args[0]);
			System.out.println("我不会唱" + args[0] + "我只是一个码农而已,我只会唱“世上只有妈妈好，有妈的孩子像个宝！”");
			return null;
		case "sign":
			System.out.println("今天终于找到工作啦，和一家软件公司签约啦！奶奶的，后来才知道原来是培训机构啊！！");
			return null;
		case "work":
			System.out.println("暂时还在工地搬砖呢");
			return null;
		case "lives":
			System.out.println("单身狗一个，毫无幸福感可言！");
			return 0;
		}
		
		return null;
	}
	
}